<template>
  <div class="backgroundDiv">
    <div style="height: 00px; width: 440px; margin-left: auto; margin-top: 20%">
      <br />
      <el-form
        ref="form"
        :model="loginData"
        label-width="80px"
        style="width: 400px"
      >
        <el-form-item label="">
          <el-input
            v-model="loginData.userName"
            placeholder="账号："
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            type="password"
            v-model="loginData.password"
            placeholder="密码："
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="loginData.code" placeholder="验证码："></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button
            type="warning"
            style="width: 320px"
            @click="myUserLoginByPassword"
            >账号登录</el-button
          >
        </el-form-item>
        <el-form-item label="">
          <el-button
            type="warning"
            style="width: 320px"
            @click="myUserLoginByCode"
            >验证码登录</el-button
          >
        </el-form-item>
        <el-form-item label="">
          <el-button type="success" style="width: 320px" @click="mySendCode"
            >发送验证码</el-button
          >
        </el-form-item>
        <el-form-item label="">
          <el-button type="success" style="width: 320px" @click="findPassword"
            >找回密码</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-dialog
        title="找回密码"
        :visible.sync="dialogFormVisible"
        width="400px"
      >
        <el-form :model="findData">
          <el-form-item label="账号：">
            <el-input v-model="findData.userName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码：">
            <el-input
              v-model="findData.password"
              autocomplete="off"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item label="验证码：">
            <el-input v-model="findData.code" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="mySendCodes">发送验证码</el-button>
          <el-button type="primary" @click="sureFind()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  userLoginByPassword,
  userLoginByCode,
  sendCode,
  findUser,
} from "@/api/userApi";
export default {
  data() {
    return {
      dialogFormVisible: false,
      findData: {
        userName: "",
        password: "",
        code: "",
      },
      loginData: {
        userName: "",
        password: "",
        code: "",
      },
    };
  },
  methods: {
    sureFind() {
      findUser(this.findData).then((result) => {
        if (result.data.flag == true) {
          this.dialogFormVisible = false;
          alert(result.data.data);
        } else {
          alert(result.data.data);
        }
      });
    },
    findPassword() {
      this.dialogFormVisible = true;
    },
    mySendCodes() {
      sendCode(this.findData).then((result) => {
        alert(result.data.data);
      });
    },
    mySendCode() {
      sendCode(this.loginData).then((result) => {
        alert(result.data.data);
      });
    },
    myUserLoginByCode() {
      userLoginByCode(this.loginData).then((result) => {
        if (result.data.flag == true) {
          window.sessionStorage.setItem("token", result.data.data.token);
          alert("登录成功");
          if (result.data.data.power == 0) {
            this.$router.push("/siteList");
          } else {
            this.$router.push("/applyList");
          }
        } else {
          alert(result.data.data);
        }
      });
    },
    myUserLoginByPassword() {
      userLoginByPassword(this.loginData).then((result) => {
        if (result.data.flag == true) {
          window.sessionStorage.setItem("token", result.data.data.token);
          alert("登录成功");
          if (result.data.data.power == 0) {
            this.$router.push("/siteList");
          } else {
            this.$router.push("/applyList");
          }
        } else {
          alert(result.data.data);
        }
      });
    },
  },
};
</script>
<style>
.backgroundDiv {
  background: url("../assets/loginBg.b3ee8a4.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 0px;
}
</style>
